// pages/frontKaoqin/frontKaoqin.js
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date1: '',
    date2: '',
    rangeKey1: 'name',
    rangeKey2: 'name',
    value1: '项目',
    value2: '班级',
    kaoqinList: []
  },


  //获取校区、项目、班级
  getSchool() {
    api.fetchApi('reception/getsearch', { token: wx.getStorageSync('token') }).then(res => {
      var classArr = res.class;
      var projectArr = res.project;
      var campusArr = res.campus;
      //console.log(campusArr, classArr, projectArr)
      this.setData({
        classArr,
        projectArr,
        campusArr
      })

    })
  },

  //获取考勤信息
  getKaoqinInfor(page) {
    var params = {
      token: wx.getStorageSync('token'),
      campus: this.data.campus,
      placement_id: this.data.placement_id,
      time: this.data.date1,
      page
    }
    api.fetchApi('reception/attendance', params).then(res => {
      var kaoqinList = this.data.kaoqinList,
        kaoqinList1 = res.data;
      if (kaoqinList1.length == 0 && page > 1) {
        //api.showToast('到底啦!', 'http://qnwmfx.laigl.com/iconimg/warn.png', 1500)
        this.setData({
          isMore: true
        })
        return
      }
      kaoqinList = [...kaoqinList, ...kaoqinList1]
      console.log(kaoqinList)
      this.setData({
        kaoqinList,
        page,
        count: res.count
      })
    })
  },


  //选择项目
  selectSchool(e) {
    var campusArr = this.data.campusArr;
    var rangeKey1 = this.data.rangeKey1;
    var nowArr = campusArr[e.detail]
    console.log(nowArr)
    this.setData({
      value1: nowArr[rangeKey1],
      campus: nowArr.id
    })
    this.setData({
      kaoqinList: []
    })
    this.getKaoqinInfor(1)
  },

  //选择班级
  selectClass(e) {
    var classArr = this.data.classArr;
    var rangeKey2 = this.data.rangeKey2;
    var nowArr = classArr[e.detail]
    console.log(nowArr)
    this.setData({
      value2: nowArr[rangeKey2],
      placement_id: nowArr.id
    })
    this.setData({
      kaoqinList: []
    })
    this.getKaoqinInfor(1)

  },

  //时间始   子组件传过来的事件
  dateChange1(e) {

    this.setData({
      date1: e.detail
    })

    this.setData({
      kaoqinList: []
    })
    this.getKaoqinInfor(1)

  },

  //时间止
  dateChange2(e) {

    this.setData({
      date2: e.detail
    })
    this.setData({
      kaoqinList: []
    })
    this.getKaoqinInfor(1)

  },

  subimitInfor() {
    var kaoqinList = this.data.kaoqinList;
    var student = [];
    var teacher = [];
    var teacherArr = []; //老师id去重后

    // var teacher_attendance_id='';

    //获取点击操作过的学生
    var kaoqinList1 = kaoqinList.filter(item => {
      return item.type_list.some((item1 => { return item1.isActive == 1 }))
    })
    console.log(kaoqinList1)



    var json = {}


    var type;

    kaoqinList1.forEach((item, idx, arr) => {

      item.type_list.forEach((item1, index, arr) => {
        if (arr.some(item2 => { return item2.isActive == 1 && item2.name == "早退" })) { type = 3 }
        if (arr.some(item2 => { return item2.isActive == 1 && item2.name == "请假" })) { type = 2 }
        if (arr.some(item2 => { return item2.isActive == 1 && item2.name == "旷课" })) { type = 1 }
        if (arr.some(item2 => { return item2.isActive == 1 && item2.name == "迟到" })) { type = 0 }
        if (arr.some(item2 => { return item2.isActive == 1 && item2.name == "正常" })) { type = 4 }
      })

      student.push({ id: item.id, type, student_id: item.student_id})

  
    })
    console.log(json)
   

    //筛选老师id不同的
    var obj = {};
    teacherArr = kaoqinList1.reduce((item, next) => {
      obj[next.teacher_attendance_id] ? '' : obj[next.teacher_attendance_id] = true && item.push(next);
      return item;
    }, [])
    console.log(teacherArr)

  
    teacherArr.forEach((item,index,arr) => {
      console.log(item.teacher_attendance_id)
      var leave_early = 0;  //早退
      var leave = 0;      //请假
      var truancy = 0; //旷课
      var late=0; //迟到
      var attend_class=0 //正常
      kaoqinList1.forEach(item1 => {
       
        if (item.teacher_attendance_id == item1.teacher_attendance_id) {
          if (item1.type_list.filter(item2 => { { return item2.isActive == 1 && item2.name == "早退" } }).length>0) { leave_early++ }
          if (item1.type_list.filter(item2 => { { return item2.isActive == 1 && item2.name == "请假" } }).length > 0) { leave++ }
          if (item1.type_list.filter(item2 => { { return item2.isActive == 1 && item2.name == "旷课" } }).length > 0) { truancy++ }
          if (item1.type_list.filter(item2 => { { return item2.isActive == 1 && item2.name == "迟到" } }).length > 0) { late++ }
          if (item1.type_list.filter(item2 => { { return item2.isActive == 1 && item2.name == "正常" } }).length > 0) { attend_class++ }
        }
      })

      teacher.push({ teacher_attendance_id: item.teacher_attendance_id, leave_early, leave, truancy, late, attend_class })
    })

    var params = {
      token: wx.getStorageSync('token'),
      teacher,
      student
    }
    console.log(params)
  
    wx.showModal({
      title: '确认提交',
      content: '确定考勤信息无误',
      success:res=> {


        if (res.confirm) {
          api.fetchApi('reception/submit_to', params).then(res=>{
            api.showToast('提交成功').then(()=>{
             
              setTimeout(()=>{
                this.setData({
                  kaoqinList:[]
                })
                // wx.navigateTo({
                //   url: '../frontKaoqin/frontKaoqin',
                // })
                this.getKaoqinInfor(1)
              },1500)
            })
          })
        }
      }
    })
  },


  //选择点击考勤
  handleKaoqin(e) {
    // var typeList = this.data.typeList;
    var num = e.currentTarget.dataset.num
    var type = e.currentTarget.dataset.type;
    var kaoqinList = this.data.kaoqinList;


    kaoqinList[num].type_list.forEach((item, index, arr) => {

      if (item.type == type) {
        if (item.isActive == 0) {

          arr.forEach(item1 => {
            item1.isActive = 0
          })
          item.isActive = 1

        } else {
          item.isActive = 0
          if (!arr.every(item1 => {
            return item1.isActive = 0
          })) {
            kaoqinList[num].status = 1
          }
        }
      }

    })

    api.fetchApi('reception/keep_status', { token: wx.getStorageSync('token'), id: kaoqinList[num].id, status: kaoqinList[num].type_list })


    this.setData({
      kaoqinList
    })





    this.setData({

      kaoqinList

    })



  },

  //点击拨打电话
  phoneCall(e){
    //console.log(e.currentTarget.dataset.phone)
    var phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone 
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var date1 = new Date().dateFormat('Y-m-d');
    var date2 = (new Date(new Date().valueOf() + 86400000)).dateFormat('Y-m-d')
    console.log(date1, date2)
    this.setData({
      date1,
      date2
    })
    this.getSchool()
    this.getKaoqinInfor(1);

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page = this.data.page || 1;
    page++;

    this.getKaoqinInfor(page)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})


// {
//   token:'123456',
//   teacher:[
//     {teacher_attend_id:1,'请假':4,'早退':555,'迟到':3},
//    { teacher_attend_id: 2,levae:5},
//   ],

//   student:[
//    {id:5,type},
//    { },
//     { },
//     { },
//     { },
//   }
// ]